﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class StateCoverForm : Form
    {
        int avpID, regID, canalID = 0, drID = 0, year, kv;

        bool IsCanal = true;

        tb_CanalState canalState;
        tb_drainageState drState;

        public StateCoverForm()
        {
            InitializeComponent();
        }

        private void StateCoverForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYearDr.Items.Add(i);
                cbYear.Items.Add(i);
            }
            cbYearDr.SelectedIndex = cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());
            year = Convert.ToInt32(cbYear.Text);
            for (int i = 0; i < 4; i++)
            {
                cbKvartal.Items.Add((i + 1).ToString() + " Квартал");
            }
            cbKvartal.SelectedIndex = 0;

            rbCanal.Checked = true;
        }

        private void rbCanal_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxCanal.Enabled = rbCanal.Checked;
            IsCanal = true;
        }

        private void rbDrainage_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxDrainage.Enabled = rbDrainage.Checked;
            IsCanal = false;
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbCanal.SelectedIndexChanged -= cbCanal_SelectedIndexChanged;
                cbCanal.DataSource = DBClass.Canals
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbCanal.DisplayMember = "Name";
                cbCanal.ValueMember = "ID";
                cbCanal.SelectedIndex = -1;
                cbCanal.Text = string.Empty;
                cbCanal.SelectedIndexChanged += new EventHandler(cbCanal_SelectedIndexChanged);

                cbDrainage.SelectedIndexChanged -= cbDrainage_SelectedIndexChanged;
                cbDrainage.DataSource = DBClass.Drainages
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbDrainage.DisplayMember = "Name";
                cbDrainage.ValueMember = "ID";
                cbDrainage.SelectedIndex = -1;
                cbDrainage.Text = string.Empty;
                cbDrainage.SelectedIndexChanged += new EventHandler(cbDrainage_SelectedIndexChanged);
            }
        }

        private void cbCanal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCanal.SelectedIndex >= 0)
            {
                canalID = Convert.ToInt32(cbCanal.SelectedValue);
                ShowBadLength();
            }
        }

        private void cbDrainage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbDrainage.SelectedIndex >= 0)
            {
                drID = Convert.ToInt32(cbDrainage.SelectedValue);
                ShowBadLength();
            }
        }

        void ShowBadLength()
        {
            if (IsCanal)
            {
                if (canalID > 0 && year > 0 && kv >= 0)
                {
                    DateTime dt = new DateTime(year, (kv * 3 + 1), 1);
                    canalState = DBClass.RESPContext.tb_CanalStates
                        .Where(wh => wh.CanalID == canalID && wh.Date == dt)
                        .SingleOrDefault();

                    if (canalState != null)
                        tbBadLength.Text = canalState.BadLength.Value.ToString();
                    else
                        tbBadLength.Text = string.Empty;
                }
            }
            else
            {
                if (drID > 0 && year > 0)
                {
                    drState = DBClass.DrainagesState
                        .Where(wh => wh.DrainageID == drID && wh.Year == year)
                        .FirstOrDefault();
                    if (drState != null)
                    {
                        tbBadLength.Text = drState.BadLength != null ? drState.BadLength.Value.ToString() : string.Empty;
                    }
                    else
                        tbBadLength.Text = string.Empty;
                }
            }
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYear.Text);
                ShowBadLength();
            }
        }

        private void cbKvartal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbKvartal.SelectedIndex >= 0)
            {
                kv = cbKvartal.SelectedIndex;
                ShowBadLength();
            }
        }

        private void cbYearDr_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYearDr.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYearDr.Text);
                ShowBadLength();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            decimal badLength;
            bool correct = tbBadLength.Text.GetDecimalOrDefault(out badLength);

            if (IsCanal)
            {
                if (canalState != null)
                {
                    canalState.BadLength = badLength;
                }
            }
            else if (drState != null)
            {
                drState.BadLength = badLength;
            }
            try
            {
                DBClass.RESPContext.SubmitChanges();
                MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
